//
//  ScriptLoadUtil.m
//  ocdemo
//
//  Created by mac on 2025/2/9.
//

#import "ScriptLoadUtil.h"
#import <Foundation/Foundation.h>
#import <React/RCTBridge+Private.h>
//#import "RCTBridge+Rnload.h"
#import "RCTBridge+CustomerBridge.h"
static const BOOL MULTI_DEBUG=YES;
static NSMutableArray*loadScripts=nil;
static RCTBridge*rctbridge=nil;
@implementation ScriptLoadUtil
+ (void)init:(RCTBridge *)bridge{
    rctbridge=bridge;
}
+ (RCTBridge *)getBridge{
    return rctbridge;
}
+(BOOL)isLoading{
    NSLog(@"Main bundle is still loading...");
    return rctbridge.isLoading ;
}
+ (BOOL)isDebugable{
    return MULTI_DEBUG ;
}
+ (BOOL)isScriptLoaded:(NSString *)moduleName{
    if (loadScripts==nil) {
        return NO;
    }
    return  [loadScripts indexOfObject:moduleName]!=NSNotFound;
}
+ (NSString *)getDownLoadedScriptPath:(NSString *)path moduleName:(NSString *)moduleName{
    NSString * subScriptDir=[@"Bundles" stringByAppendingPathComponent:moduleName];
    NSString * bundlePath=[[NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:subScriptDir];
    NSString * scriptPath=[bundlePath stringByAppendingPathComponent:path];
    return scriptPath;
}
+ (NSString *)getDownLoadedScriptDirWithModuleName:(NSString *)moduleName{
    NSString * subScriptDir=[@"Bundles" stringByAppendingPathComponent:moduleName];
    NSString * bundlePath=[[NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:subScriptDir];
    return  bundlePath;
}
+ (void)loadScriptWithBridge:(RCTBridge *)bridge path:(NSString *)path moduleName:(NSString *)moduleName mainBundle:(BOOL)inMain{
    if(loadScripts==nil){
        loadScripts=[NSMutableArray array];
    }
    if([loadScripts indexOfObject:moduleName]==NSNotFound){
        [loadScripts addObject:moduleName];
        NSString * jsCodeLocationBuz=nil;
        NSURL*r1=nil;
        if(inMain == YES){
            
            NSString *fileName = [path stringByDeletingPathExtension]; // 获取文件名（不包含扩展名）
            NSString *fileExtension = [path pathExtension]; // 获取扩展名
            r1= [[NSBundle mainBundle] URLForResource:fileName withExtension:fileExtension];
            r1.path;
//            jsCodeLocationBuz=[[NSBundle mainBundle] URLForResource:@"RnIndex" withExtension:@"jsbundle"];
        }else{
            NSString *scriptPath=[self getDownLoadedScriptPath:path moduleName:moduleName];
            jsCodeLocationBuz=scriptPath;
            r1=[NSURL URLWithString:scriptPath ];
        }
        
        NSError * error=nil;
        NSData* sourceBuz=[NSData dataWithContentsOfFile:r1.path  options:NSDataReadingMappedIfSafe error:&error];
     
//        RCTBridge 是一个基类，RCTCxxBridge 继承它并扩展了更多功能。
//        代码通常使用 RCTBridge 作为引用类型，但在运行时会实际使用子类（如 RCTCxxBridge）的实例。
//        [bridge.batchedBridge executeSourceCode:sourceBuz sync:NO];
        [bridge.batchedBridge executeSourceCode:sourceBuz withSourceURL:r1 sync:NO];
            
    }
}
@end
